package com.eric.windows.menu;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.eric.windows.R;

public class XMLMenuTestActivity extends AppCompatActivity {
    private TextView txt_menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_test);
        txt_menu = (TextView) findViewById(R.id.txt_menu);
        txt_menu.setText("菜单测试");
        //注册上下文菜单 注册后，长按txt_menu 会弹出上文按钮
        registerForContextMenu(txt_menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = new MenuInflater(this);
        inflater.inflate(R.menu.normal_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.fount_10:
                txt_menu.setTextSize(10*2);
            break;
            case R.id.fount_12:
                txt_menu.setTextSize(12*2);
            break;
            case R.id.fount_14:
                txt_menu.setTextSize(14*2);
                break;
            case R.id.fount_16:
                txt_menu.setTextSize(16*2);
                break;
            case R.id.fount_18:
                txt_menu.setTextSize(18*2);
                break;
            case R.id.f_color_17:
                txt_menu.setTextColor(Color.RED);
                break;
            case R.id.f_color_18:
                txt_menu.setTextColor(Color.BLUE);
                break;
            case R.id.f_color_19:
                txt_menu.setTextColor(Color.GREEN);
                break;

        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflater  = new MenuInflater(this);
        inflater.inflate(R.menu.context_menu,menu);
        //添加上下菜单
        menu.setHeaderIcon(R.mipmap.ic_launcher);
        menu.setHeaderTitle("选择背景色");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.b_color_17:
                txt_menu.setBackgroundColor(Color.RED);
                break;
            case R.id.b_color_18:
                txt_menu.setBackgroundColor(Color.BLUE);
                break;
            case R.id.b_color_19:
                txt_menu.setBackgroundColor(Color.GREEN);
                break;

        }
        return super.onContextItemSelected(item);
    }
}
